package com.learning.js_cookieandsession;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet(name = "SessionServlet1", value = "/demo2")
public class MySession extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        HttpSession session = req.getSession();

        // 示例：设置属性
        session.setAttribute("age", "18");
        session.setAttribute("username", "John");

// 示例：获取属性
        String username = (String) session.getAttribute("username");

// 示例：删除属性
        session.removeAttribute("username");
        System.out.println(session.getAttribute("age"));
        System.out.println("Session id :" + session.getId());

        res.sendRedirect("MySession.jsp");
    }
}
